package com.it.gw.modules.station.activity

import ADNMVo
import DataSourceVo
import GWRunningStaticVo
import LoginVo
import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.Toast
import com.it.common.utils.DateUtils
import com.it.common.utils.DisplayUtil
import com.it.common.utils.RvSpacesItemUtils
import com.it.common.utils.isNotEmpty
import com.it.data.manager.AppCacheManager
import com.it.data.network.rsp.NodeVo
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityGwRunningStatisticsBinding
import com.it.gw.databinding.LayoutPopAddvcdSelectBinding
import com.it.gw.databinding.LayoutPopDataSourceSelectBinding
import com.it.gw.databinding.LayoutPopShiSelectBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.station.adapter.GWRunningAdapter
import com.it.gw.modules.water.adapter.ADDVCDAdapter
import com.it.gw.modules.water.adapter.DataSourceAdapter
import com.it.gw.modules.water.adapter.ShiListAdapter
import com.tencent.mmkv.MMKV


//设备运行情况统计
class GWRunningStatisticsActivity :
    BaseActivity<ActivityGwRunningStatisticsBinding>() {

    companion object {
        const val PAGE_SIZE = 20
        const val TAG = "GWRunningStatisticsActivity"
    }

    private val datas by lazy { mutableListOf<GWRunningStaticVo>() }
    private val adapter by lazy {
        GWRunningAdapter(context = this, datas = datas, listener = {

        })
    }


    override fun initObserver() {

        gqStationViewModel.gwRunningStaticVoListLiveData.observe(this) {
            binding.swipeRefreshLayout.isRefreshing = false
            datas.clear()
            if (it.result && it.data.isNotEmpty()) {
                datas.addAll(it.data!!)
                Log.d(TAG, "initObserver: gwRunningStaticVoListLiveData ${datas}")
            }
            else{
                Toast.makeText(this,"${it.msg}",Toast.LENGTH_LONG).show()
            }
            adapter.notifyDataSetChanged()
            ProgressDialog.hide()

        }

    }


    @SuppressLint("RestrictedApi")
    override fun initListener() {
        binding.rlSelect1.setOnClickListener {
            ProgressDialog.show(this)
            val loginVo = MMKV.defaultMMKV()
                .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)
            mainViewModel.getNodeByAd(depId = loginVo?.depId!!) {
                runOnUiThread {
                    ProgressDialog.hide()
                    if (it.result) {
                        val shiList = it.data?.filter { it.pid == "660000000" }
                        if (shiList.isNotEmpty()) {
                            showSelectShiTuanPopupWindow(binding.rlTop, shiList!!)
                        }

                    } else {
                        Toast.makeText(this, it.msg, Toast.LENGTH_LONG).show()
                    }

                }
            }
        }
        binding.rlSelect2.setOnClickListener {
            showSelectDataSourcePopupWindow(binding.rlTop, dataSourceVoList)
        }
        binding.swipeRefreshLayout.setOnRefreshListener {
            resetView()
            getData()
        }

    }

    private var dataSourceAdapter: DataSourceAdapter? = null
    private fun showSelectDataSourcePopupWindow(
        anchor: LinearLayout,
        dataSourceVoList: MutableList<DataSourceVo>
    ) {
        val dataSourceSelectBinding = LayoutPopDataSourceSelectBinding.inflate(layoutInflater)
        val popupWindow = PopupWindow(
            dataSourceSelectBinding.root,
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        val map = HashMap<String, Int>()
        //间距
        map[RvSpacesItemUtils.BOTTOM_DECORATION] = 1
        dataSourceSelectBinding.rvList.addItemDecoration(RvSpacesItemUtils(map))
        dataSourceAdapter = DataSourceAdapter(datas = dataSourceVoList, context = this, listener = {
            it.select = !it.select
            dataSourceAdapter?.notifyDataSetChanged()


        })
        dataSourceSelectBinding.rvList.adapter = dataSourceAdapter


        // 设置 PopupWindow 的其他属性
        popupWindow.isFocusable = true
        dataSourceSelectBinding?.vOutSide?.setOnClickListener {
            popupWindow.dismiss()
        }
        dataSourceSelectBinding?.rlCancel?.setOnClickListener {
            popupWindow.dismiss()
        }
        dataSourceSelectBinding?.rlOk?.setOnClickListener {
            popupWindow.dismiss()
            ProgressDialog.show(this)
            getData()
        }
        // 显示 PopupWindow
        popupWindow.showAsDropDown(anchor)
    }


    override fun initData() {
        ProgressDialog.show(this)
        getData()


    }


    private val dataSourceVoList = mutableListOf(
        DataSourceVo(code = "0", name = "山洪站", select = true),
        DataSourceVo(code = "1", name = "中小河流站", select = true),
        DataSourceVo(code = "6", name = "防汛抗旱", select = true),
        DataSourceVo(code = "8", name = "地方", select = true),
        DataSourceVo(code = "10", name = "国控一期水位站", select = true),
        DataSourceVo(code = "11", name = "国控一期管道站", select = true),
        DataSourceVo(code = "12", name = "国控二期水位站", select = true),
        DataSourceVo(code = "13", name = "国控二期管道流量站", select = true),
        DataSourceVo(code = "14", name = "视频站", select = true),
        DataSourceVo(code = "15", name = "图像站", select = true),
        DataSourceVo(code = "9", name = "其他", select = true),
    )

    private var trcd: String? = "660000000"
    private fun getData() {
        val loginVo = MMKV.defaultMMKV()
            .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)
        val comments = dataSourceVoList.filter { it.select }.map { it.code }.joinToString(",")
        gqStationViewModel.OperationStatistics(
            userId = loginVo?.id,
            trcd = trcd,
            onlineRate = "0.8",
            comments = comments
        )


    }

    override fun initView() {
        setTitle(
            title = R.string.str_gw_running_static,
            isBack = true,
        )

        val map = hashMapOf(RvSpacesItemUtils.BOTTOM_DECORATION to DisplayUtil.dip2px(this, 8f))
        binding.rvList.addItemDecoration(RvSpacesItemUtils(map))
        binding.rvList.adapter = adapter


        binding.t1.text = DateUtils.getToday()

    }

    override fun setContentView() {
        binding = ActivityGwRunningStatisticsBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }


    private lateinit var shiAdapter: ShiListAdapter

    private lateinit var addvcdSelectBinding: LayoutPopShiSelectBinding

    private fun showSelectShiTuanPopupWindow(anchor: View, shiList: List<NodeVo>) {
        addvcdSelectBinding = LayoutPopShiSelectBinding.inflate(layoutInflater)
        val popupWindow = PopupWindow(
            addvcdSelectBinding.root,
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        val map = HashMap<String, Int>()
        //间距
        map[RvSpacesItemUtils.BOTTOM_DECORATION] = 1
        addvcdSelectBinding.rvShi.addItemDecoration(RvSpacesItemUtils(map))
        shiAdapter = ShiListAdapter(datas = shiList, context = this, listener = {
            selectShi(it)
            popupWindow.dismiss()
            shiAdapter.notifyDataSetChanged()

        })
        addvcdSelectBinding.rvShi.adapter = shiAdapter


        // 设置 PopupWindow 的其他属性
        popupWindow.isFocusable = true
        addvcdSelectBinding?.vOutSide?.setOnClickListener {
            popupWindow.dismiss()
        }
        addvcdSelectBinding?.rlCancel?.setOnClickListener {
            popupWindow.dismiss()
        }

        // 显示 PopupWindow
        popupWindow.showAsDropDown(anchor)

    }


    private var adnmShiVo: NodeVo? = null
    private fun selectShi(vo: NodeVo) {
        adnmShiVo = vo
        setXZQHView()
        ProgressDialog.show(this)
        Log.d(TAG, "selectShi: ======================")
        getData()


    }

    private fun setXZQHView() {
        if (!adnmShiVo.isNotEmpty()) {
            binding.tvSelect1.text = "行政区划"
            trcd = "660000000"
        } else {
            binding.tvSelect1.text = "${adnmShiVo?.text}"
            trcd = adnmShiVo?.id
        }

    }

    private fun resetView() {
        binding.tvSelect1.text = "行政区划"
        binding.tvSelect2.text = "数据来源"
        trcd = "660000000"
        dataSourceVoList.forEach {
            it.select = true
        }
    }

}